﻿using Dm;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using S.EarTag.Domain.Model;
using SqlSugar;

namespace S.EarTag.Domain.DataAccess
{
    public class MyDbContext : DbContext
    {
        private readonly IConfiguration _configuration;
        
        public MyDbContext(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        public DbSet<AppearanceModel>? AppearanceModel { get; set; }
        public DbSet<BodyMeasureModel>? BodyMeasureModel { get; set; }
        public DbSet<EarTagModel>? EarTagModel { get; set; }
        public DbSet<EarTagOrMarkModel>? EarTagOrMarkModel { get; set; }
        public DbSet<MarkModel>? MarkModel { get; set; }
        public DbSet<BreedingWeigh>? BreedingWeigh { get; set; }
        public DbSet<ProductionGrading>? ProductionGrading { get; set; }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            //声明实体类的主键Id不然迁移时会报错
            modelBuilder.Entity<AppearanceModel>().HasKey(a => a.AppearanceId);
            modelBuilder.Entity<BodyMeasureModel>().HasKey(a => a.BodyMeasureId);
            modelBuilder.Entity<EarTagModel>().HasKey(a => a.EarTagId);
            modelBuilder.Entity<EarTagOrMarkModel>().HasKey(a => a.EarTagOrMarkId);
            modelBuilder.Entity<MarkModel>().HasKey(a => a.MarkId);
            modelBuilder.Entity<BreedingWeigh>().HasKey(a => a.BreedingID);
            modelBuilder.Entity<ProductionGrading>().HasKey(a => a.ProductionGradingId);

            //声明实体类中decimal类型的字段
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.Height).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.BodyLength).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.Chest).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.Tube).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.Oschea).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.Backfat).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.EyeMuscleArea).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BodyMeasureModel>().Property(e => e.EyeMuscleWidth).HasColumnType("decimal(18,2)");
            modelBuilder.Entity<BreedingWeigh>().Property(e => e.Weight).HasColumnType("decimal(18,2)");

            //调用基础方法
            base.OnModelCreating(modelBuilder);
        }

        //连接数据库字符串
        private static readonly string ConnectionString = "server=152.136.226.135;pwd=SERVER@bawei963;uid=sa;database=SheepStorage";
        public static SqlSugarClient CreateDbOmstance()
        {
            var db = new SqlSugarClient(new ConnectionConfig
            {
                ConnectionString = ConnectionString,
                DbType = DbType.SqlServer,
                IsAutoCloseConnection = true,
                InitKeyType = InitKeyType.Attribute
            });
            //启用CodeFirst功能
            db.CodeFirst.InitTables(typeof(AppearanceModel));

            return db;
        }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            string connectionString = _configuration["ConnectionString"];
            optionsBuilder.UseSqlServer(connectionString);
        }
    }
}
